package com.wutianyi.module.services.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;

import com.wutianyi.module.product.ProductAttrDao;
import com.wutianyi.module.product.ProductDao;
import com.wutianyi.module.product.ProductInfoDao;
import com.wutianyi.module.product.dataobject.ProductAttrDO;
import com.wutianyi.module.product.dataobject.ProductDO;
import com.wutianyi.module.product.dataobject.ProductDTO;
import com.wutianyi.module.product.dataobject.ProductInfoDO;
import com.wutianyi.module.services.ProductServices;

/**
 * @author wutianyi
 * 
 */
public class ProductServicesImpl implements ProductServices {
	
	private ProductDao productDao;
	private ProductAttrDao productAttrDao;
	private ProductInfoDao productInfoDao;
	
	@Override
	public ProductDTO getProductDTO(int cId, int id) {
		ProductDO product = productDao.getProduct(cId, id);
		
		if (null == product) {
			return null;
		}
		
		List<ProductAttrDO> productAttrs = productAttrDao.listProductAttrsByProductId(id);
		ProductInfoDO productInfo = productInfoDao.getProductInfoByProductId(id);
		
		return new ProductDTO(product, productAttrs, productInfo);
	}
	
	@Override
	public int createProduct(ProductDTO product) {
		if (null == product) {
			return 0;
		}
		productDao.createProduct(product.getProduct());
		
		int productId = product.getProduct().getId();
		if (null != product.getAttrs()) {
			for (ProductAttrDO productAttr : product.getAttrs()) {
				productAttr.setProductId(productId);
			}
		}
		
		productAttrDao.createProductAttrs(product.getAttrs());
		
		product.getProductInfo().setProductId(productId);
		
		productInfoDao.createProductInfo(product.getProductInfo());
		return 1;
	}
	
	public ProductDao getProductDao() {
		return productDao;
	}
	
	public void setProductDao(ProductDao productDao) {
		this.productDao = productDao;
	}
	
	public ProductAttrDao getProductAttrDao() {
		return productAttrDao;
	}
	
	public void setProductAttrDao(ProductAttrDao productAttrDao) {
		this.productAttrDao = productAttrDao;
	}
	
	public ProductInfoDao getProductInfoDao() {
		return productInfoDao;
	}
	
	public void setProductInfoDao(ProductInfoDao productInfoDao) {
		this.productInfoDao = productInfoDao;
	}
	
	@Override
	public List<ProductDO> listProductsByCompanyId(int companyId, int pageNo, int pageSize) {
		return productDao.listProductsByCompanyId(companyId, pageNo, pageSize);
	}
	
	@Override
	public List<ProductDTO> listProductDTOsByCompanyId(int companyId) {
		return listProductDTOsByCompanyId(companyId, 1, Integer.MAX_VALUE);
	}
	
	@Override
	public int listProductCountByCompanyId(int companyId) {
		return productDao.listProductCountByCompanyId(companyId);
	}
	
	@Override
	public int updateProduct(ProductDTO product) {
		ProductDTO orignal = getProductDTO(product.getProduct().getCompanyId(), product
				.getProduct().getId());
		if (null == orignal) {
			return 0;
		}
		if (orignal.getProductInfo() == null) {
			product.getProductInfo().setProductId(product.getId());
			productInfoDao.createProductInfo(product.getProductInfo());
		} else {
			product.getProductInfo().setId(orignal.getProductInfo().getId());
			productInfoDao.updateProductInfo(product.getProductInfo());
		}
		
		productDao.updateProduct(product.getProduct());
		
		productAttrDao.deleteProductAttrsByPId(orignal.getProduct().getId());
		
		if (null != product.getAttrs()) {
			for (ProductAttrDO productAttr : product.getAttrs()) {
				productAttr.setProductId(orignal.getProduct().getId());
			}
		}
		productAttrDao.createProductAttrs(product.getAttrs());
		return 1;
	}
	
	@Override
	public List<ProductDTO> listProductDTOsByCompanyId(int companyId, int pageNo, int pageSize) {
		List<ProductDO> products = productDao.listProductsByCompanyId(companyId, pageNo, pageSize);
		
		List<ProductDTO> ps = new ArrayList<ProductDTO>();
		if (CollectionUtils.isNotEmpty(products)) {
			for (ProductDO product : products) {
				List<ProductAttrDO> productAttrs = productAttrDao
						.listProductAttrsByProductId(product.getId());
				ps.add(new ProductDTO(product, productAttrs, null));
			}
		}
		
		return ps;
	}
	
	@Override
	public List<ProductDO> listLastestProducts(int count) {
		return productDao.listLastestProducts(count);
	}
	
}
